// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "user_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "用户",
    "title": "用户ID"
  },
  "date": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "label": "日期",
    "title": "异常日期"
  },
  "type": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "late",
            "text": "late"
          },
          {
            "value": "early",
            "text": "early"
          },
          {
            "value": "absent",
            "text": "absent"
          },
          {
            "value": "no_punch",
            "text": "no_punch"
          },
          {
            "value": "location_mismatch",
            "text": "location_mismatch"
          }
        ]
      }
    ],
    "label": "类型",
    "title": "异常类型"
  },
  "reason": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "原因",
    "title": "异常原因"
  },
  "duration": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "异常时长",
    "label": "异常时长"
  },
  "attachments": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      }
    ],
    "title": "附件",
    "label": "附件"
  },
  "status": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "pending",
            "text": "pending"
          },
          {
            "value": "approved",
            "text": "approved"
          },
          {
            "value": "rejected",
            "text": "rejected"
          }
        ]
      }
    ],
    "label": "状态",
    "title": "处理状态",
    "defaultValue": "pending"
  },
  "approver_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "审批人",
    "label": "审批人"
  },
  "approve_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "审批时间",
    "label": "审批时间"
  },
  "approve_remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "审批备注",
    "label": "审批备注"
  }
}

const enumConverter = {
  "type_valuetotext": {
    "late": "late",
    "early": "early",
    "absent": "absent",
    "no_punch": "no_punch",
    "location_mismatch": "location_mismatch"
  },
  "status_valuetotext": {
    "pending": "pending",
    "approved": "approved",
    "rejected": "rejected"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
